package com.sun.web.exception;

import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by pullFoot_youth on 2017/3/12.
 */
@ControllerAdvice
public class DefaultExceptionHandler {


    @ExceptionHandler
    public ModelAndView defaulMethod(NativeWebRequest request,Exception ex){


        CustomException customException = null;

        if(ex instanceof CustomException){

            customException = (CustomException)ex;
        }else{
            customException = new CustomException(ex.getMessage());
        }

        String msg = customException.getMessage();

        ModelAndView mav = new ModelAndView();

        mav.setViewName("/error");
        mav.addObject("message", msg);

        return mav;

    }
}
